import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Message } from '../../entity/message/message.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { InsertMessageDto } from '../../entity/message/dto';

@Injectable()
export class MessageService {
  constructor(
    @InjectRepository(Message) private messageRepository: Repository<Message>,
  ) {}

  /**
   * 数据库查询未被删除的消息数据
   */
  getMessageList() {
    const result = this.messageRepository.find({
      select: {
        id: true,
        sendUser: true,
        messageInfo: true,
      },
      where: {
        deleted: 0,
      },
      order: {
        createTime: 'ASC',
      },
    });
    return result;
  }

  /**
   *
   * @param messageInfo 聊天信息
   * @returns boolean 是否添加成功
   */
  insertMessage(messageInfo: InsertMessageDto) {
    const result = this.messageRepository.save(messageInfo);
    return result;
  }
}
